package com.yuncool.sky.api;

import com.yuncool.sky.system.domain.User;
import com.yuncool.sky.system.pojo.bo.CourseBO;
import com.yuncool.sky.system.pojo.bo.SubjectBO;
import com.yuncool.sky.system.pojo.form.SubjectForm;
import com.yuncool.sky.system.service.CourseClassifyService;
import com.yuncool.sky.system.service.SubjectService;
import com.yuncool.sky.system.service.SysParamService;
import com.yuncool.sky.system.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Auther XIE
 * @Date 2019/11/23 11:24
 * @Description 前端API控制器
 * @Version 1.0
 */
@RestController
@RequestMapping("api")
public class SkyController {

    @Autowired
    UserService userService;
    @Autowired
    CourseClassifyService courseClassifyService;
    @Autowired
    SubjectService subjectService;
    @Autowired
    SysParamService sysParamService;

    /**
     * 用户登陆接口
     *
     * @param user
     * @return
     */
    @RequestMapping("login")
    public String login(@RequestBody User user) {
        return userService.selectUser(user);
    }

    /**
     * 课程分类接口
     *
     * @return
     */
    @RequestMapping("studyList")
    public List<CourseBO> studyList() {
        subjectService.cacheData();
        return courseClassifyService.studyList();
    }

    /**
     * 课程接口
     *
     * @param subjectForm
     * @return
     */
    @RequestMapping("subjectList")
    public List<SubjectBO> subjectList(@RequestBody SubjectForm subjectForm) {
        return subjectService.subjectList(subjectForm.getQuery());
    }

    /**
     * 选课API
     *
     * @param subjectForm
     * @return
     */
    @RequestMapping("selectSubject")
    public Map<String, Object> selectSubject(@RequestBody SubjectForm subjectForm) {
        Map<String, Object> mav = new HashMap<>();
        //先检查是否已经抢课了
        Boolean checkFlag = subjectService.checkRecord(subjectForm.getUserNumber());
        if (checkFlag) {
            mav.put("code", "01");
            mav.put("message", "已经抢过课了~");
            return mav;
        }
        mav = subjectService.selectSubject(subjectForm);
        return mav;
    }

    /**
     * 选课结束时间
     *
     * @return
     */
    @RequestMapping("endTime")
    public String getEndTime() {
        return sysParamService.getEndTime();
    }

    /**
     * 选课开始时间
     *
     * @return
     */
    @RequestMapping("startTime")
    public String getStartTime() {
        return sysParamService.getStartTime();
    }
}
